package com.goldcn.order.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goldcn.common.CommonTools;
import com.goldcn.common.HttpUtil;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.model.DataResult;
import com.goldcn.config.ProConstants;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.goods.dao.GoodsInfoMapper;
import com.goldcn.goods.model.GoodsInfo;
import com.goldcn.interceptor.SystemContext;
import com.goldcn.order.dao.OrderInfoMapper;
import com.goldcn.order.dao.OrderItemMapper;
import com.goldcn.order.dto.BranchReportFormDto;
import com.goldcn.order.dto.GoodsReportDto;
import com.goldcn.order.dto.MemberReportDto;
import com.goldcn.order.vo.BranchReportVo;
import com.goldcn.order.vo.GoodsReportAllVo;
import com.goldcn.order.vo.MemberSalesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/4/13.
 */
@Service
public class GoodsReportService {
    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    GoodsInfoMapper goodsInfoMapper;

    @Autowired
    OrderInfoMapper orderInfoMapper;

    @Autowired
    RestTemplate restTemplate;


    /**
     * 在售商品总数
     * @return
     */
    public Object getOnSalesCounts(){
        Integer counts =  goodsInfoMapper.getOnSalesCounts(SystemContext.getCompanyID());
        Map<String,Object> map = new HashMap<>();
        map.put("onSalesCounts",counts);
        return map;
    }


    /**
     *
     * @param goodsReportDto
     * @return
     */
    public Object getAllGoodsReport(GoodsReportDto goodsReportDto){
        PageHelper.startPage(goodsReportDto.getPageNo(), goodsReportDto.getPageSize());
        goodsReportDto.setCompanyID(SystemContext.getCompanyID());
        List<GoodsReportAllVo> getAllReportList = orderItemMapper.getGoodsReportAll(goodsReportDto);
        List<GoodsReportAllVo> list = this.getGoodsInfo(getAllReportList);
        PageInfo<List<BranchReportVo>> page = new PageInfo(list);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("list",page);
        map.put("pageCounts",page.getTotal());
        return map;
    }

    /**导出
     * 商品统计主页面 按商品分统计
     * @param goodsReportDto
     * @return
     */
    public Object getAllGoodsReportExport(GoodsReportDto goodsReportDto){
        goodsReportDto.setCompanyID(SystemContext.getCompanyID());
        List<GoodsReportAllVo> getAllReportList = orderItemMapper.getGoodsReportAll(goodsReportDto);
        return this.getGoodsInfo(getAllReportList);
    }

    /**
     * 报表第一个部门的统计
     * @param branchReportFormDto
     * @return
     */
    public Object getOrderListPortHeader(BranchReportFormDto branchReportFormDto){
        if(branchReportFormDto.getSearchName() != null && !"".equals(branchReportFormDto.getSearchName())){
            DataResult dataResult = HttpUtil.httpGet(ProConstants.getOrgListByLikeName+branchReportFormDto.getOrganizationalId()+"/"+branchReportFormDto.getSearchName(), restTemplate);
            List<Long> orgList = (List<Long>)dataResult.getData();
            if(orgList!=null && orgList.size()!=0){
                branchReportFormDto.setSearchIdList(orgList);
            }else{
//                branchReportFormDto.setSearchIdList(null);
                return new ArrayList<>();
            }
        }
        String goodsId = branchReportFormDto.getGoodsId();
        if(goodsId==null || "".equals(goodsId)){
            throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        int roleType = branchReportFormDto.getRoleType();
        List<BranchReportVo> list = null;
        //设置分页处理
        PageHelper.startPage(branchReportFormDto.getPageNo(),branchReportFormDto.getPageSize());
        switch (roleType){
            case 1:
                branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
                list =  orderInfoMapper.reportHeaderGoods(branchReportFormDto);
                break;
            case 2:
                list = orderInfoMapper.reportHeaderTwoGoods(branchReportFormDto);
                break;
            case 3:
                list = orderInfoMapper.reportHeaderThirGoods(branchReportFormDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        List<Long> idList = new ArrayList<>(); //查询出来的id作为一个集合 查询出名字
        if(list != null && list.size()!=0){
            for(BranchReportVo vo:list){
                if("0".equals(vo.getOrganizationId())){
                    vo.setOrganizationId(branchReportFormDto.getOrganizationalId()+"");
                }
                idList.add(Long.parseLong(vo.getOrganizationId()));
            }
            DataResult dataResult = HttpUtil.httpPost(ProConstants.getBranchListAllState,idList, restTemplate);
            if(dataResult.getIsSuccess()!=1){
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
            }
            Map<String,Object> nameMap = ( Map<String,Object>)dataResult.getData();
            for(BranchReportVo vo:list){
                String benDepart = branchReportFormDto.getOrganizationalId()+"";
                if(benDepart.equals(vo.getOrganizationId())){
//                    if(roleType==1){
                        vo.setOrganizationName( nameMap.get(vo.getOrganizationId())+"(本部)");
//                    }else{
//                        vo.setOrganizationName( nameMap.get(vo.getOrganizationId())+"");
//                    }

                    vo.setIsSelf("1");
                }else{
                    vo.setOrganizationName((String)nameMap.get(vo.getOrganizationId()));
                    vo.setIsSelf("0");
                }
                vo.setLevel((Integer) nameMap.get(vo.getOrganizationId() + "_level"));
                vo.setPath((String)nameMap.get(vo.getOrganizationId()+"_path"));
                vo.setHasNext((String)nameMap.get(vo.getOrganizationId()+"_hasNext"));
            }
        }
//        DataResult dataResult2 = HttpUtil.httpGet(ProConstants.BASE+"/userinfo/getLevel1Id",DataResult.class);
//        HttpUtil.httpGet(ProConstants.BASE+"/userinfo/getLevel1Id",DataResult.class);
//        Long levelId = (Long)dataResult2.getData();

        PageInfo<List<BranchReportVo>> page = new PageInfo(list);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("list",page);
        map.put("pageCounts",list.size());
//        map.put("levelId",levelId);
        return map;
    }

    public Object getOrderListPortHeaderExport(BranchReportFormDto branchReportFormDto){
        if(branchReportFormDto.getSearchName() != null && !"".equals(branchReportFormDto.getSearchName())){
            DataResult dataResult = HttpUtil.httpGet(ProConstants.getOrgListByLikeName+branchReportFormDto.getOrganizationalId()+"/"+branchReportFormDto.getSearchName(), restTemplate);
            List<Long> orgList = (List<Long>)dataResult.getData();
            if(orgList!=null && orgList.size()!=0){
                branchReportFormDto.setSearchIdList(orgList);
            }else{
//                branchReportFormDto.setSearchIdList(null);
                return new ArrayList<>();
            }
        }
        String goodsId = branchReportFormDto.getGoodsId();
        if(goodsId==null || "".equals(goodsId)){
            throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        int roleType = branchReportFormDto.getRoleType();
        List<BranchReportVo> list = null;
        //设置分页处理
        switch (roleType){
            case 1:
                branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
                list =  orderInfoMapper.reportHeaderGoods(branchReportFormDto);
                break;
            case 2:
                list = orderInfoMapper.reportHeaderTwoGoods(branchReportFormDto);
                break;
            case 3:
                list = orderInfoMapper.reportHeaderThirGoods(branchReportFormDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        List<Long> idList = new ArrayList<>(); //查询出来的id作为一个集合 查询出名字
        if(list != null && list.size()!=0){
            for(BranchReportVo vo:list){
                if("0".equals(vo.getOrganizationId())){
                    vo.setOrganizationId(branchReportFormDto.getOrganizationalId()+"");
                }
                idList.add(Long.parseLong(vo.getOrganizationId()));
            }
            DataResult dataResult = HttpUtil.httpPost(ProConstants.getBranchListAllState,idList, restTemplate);
            if(dataResult.getIsSuccess()!=1){
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
            }
            Map<String,Object> nameMap = ( Map<String,Object>)dataResult.getData();
            for(BranchReportVo vo:list){
                String benDepart = branchReportFormDto.getOrganizationalId()+"";
                if(benDepart.equals(vo.getOrganizationId())){
//                    if(roleType==1){
                    vo.setOrganizationName( nameMap.get(vo.getOrganizationId())+"(本部)");
//                    }else{
//                        vo.setOrganizationName( nameMap.get(vo.getOrganizationId())+"");
//                    }

                    vo.setIsSelf("1");
                }else{
                    vo.setOrganizationName((String)nameMap.get(vo.getOrganizationId()));
                    vo.setIsSelf("0");
                }
                vo.setLevel((Integer) nameMap.get(vo.getOrganizationId() + "_level"));
                vo.setPath((String)nameMap.get(vo.getOrganizationId()+"_path"));
                vo.setHasNext((String)nameMap.get(vo.getOrganizationId()+"_hasNext"));
            }
        }
        return list;
    }

    /**
     * 报表支部  根据goodsId
     * @param branchReportFormDto
     * @return
     */
    public Object getOrderListBranch(BranchReportFormDto branchReportFormDto){
        String goodsId = branchReportFormDto.getGoodsId();
        int roleType = branchReportFormDto.getRoleType();
        if((roleType != 1 && roleType != 2 && roleType != 3)||goodsId==null || "".equals(goodsId)){
            throw CommonTools.createException(ErrorCodeEnum.queryBranchFail);
        }
        List<BranchReportVo> list = new ArrayList<>();
        //设置分页处理
        PageHelper.startPage(branchReportFormDto.getPageNo(),branchReportFormDto.getPageSize());
        switch (roleType){
            case 1:
                branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.reportHeadquartersGoods(branchReportFormDto);
                break;
            case 2:
                list = orderInfoMapper.reportBranchGoods(branchReportFormDto);
                break;
            case 3:
                list = orderInfoMapper.reportTwoLevelBranchIDGoods(branchReportFormDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        //此时我已经得到了各个部门的统计数据了 还欠缺一个名字
        List<Long> idList = new ArrayList<>();
        for(BranchReportVo vo:list){
            OrderReportFormService.getPortData(vo,roleType,branchReportFormDto);
            idList.add(Long.parseLong(vo.getOrganizationId()));
            System.out.print(Long.parseLong(vo.getOrganizationId())+",");
        }

        if(idList == null || idList.size() == 0){
            Map<String,Object> map = new HashMap<String, Object>();
            map.put("list",new PageInfo(list));
            map.put("pageCounts",0);
            return map;
        }
        DataResult dataResult = HttpUtil.httpPost(ProConstants.getBranchListAllState,idList, restTemplate);
        Map<String,Object> nameMap = ( Map<String,Object>)dataResult.getData();
        for(BranchReportVo vo:list){
            String benDepart = branchReportFormDto.getOrganizationalId()+"";
            if(benDepart.equals(vo.getOrganizationId())){
                if(roleType==1){
                    vo.setOrganizationName( nameMap.get(vo.getOrganizationId())+"(本部)");
                }else{
                    vo.setOrganizationName( nameMap.get(vo.getOrganizationId())+"");
                }
            }else{
                vo.setOrganizationName((String)nameMap.get(vo.getOrganizationId()));
            }
            vo.setLevel((Integer) nameMap.get(vo.getOrganizationId() + "_level"));
            vo.setIsSelf("1");
            vo.setPath((String)nameMap.get(vo.getOrganizationId()+"_path"));
        }

        PageInfo<List<BranchReportVo>> page = new PageInfo(list);
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("list",page);
        map.put("pageCounts",page.getTotal());
        return map;
    }

    public Object getOrderListBranchExport(BranchReportFormDto branchReportFormDto){
        String goodsId = branchReportFormDto.getGoodsId();
        int roleType = branchReportFormDto.getRoleType();
        if((roleType != 1 && roleType != 2 && roleType != 3)||goodsId==null || "".equals(goodsId)){
            throw CommonTools.createException(ErrorCodeEnum.queryBranchFail);
        }
        List<BranchReportVo> list = new ArrayList<>();
        //设置分页处理
        switch (roleType){
            case 1:
                branchReportFormDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.reportHeadquartersGoods(branchReportFormDto);
                break;
            case 2:
                list = orderInfoMapper.reportBranchGoods(branchReportFormDto);
                break;
            case 3:
                list = orderInfoMapper.reportTwoLevelBranchIDGoods(branchReportFormDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        //此时我已经得到了各个部门的统计数据了 还欠缺一个名字
        List<Long> idList = new ArrayList<>();
        for(BranchReportVo vo:list){
            OrderReportFormService.getPortData(vo,roleType,branchReportFormDto);
            idList.add(Long.parseLong(vo.getOrganizationId()));
            System.out.print(Long.parseLong(vo.getOrganizationId())+",");
        }

        if(idList == null || idList.size() == 0){
            return list;
        }
        DataResult dataResult = HttpUtil.httpPost(ProConstants.getBranchListAllState,idList, restTemplate);
        Map<String,Object> nameMap = ( Map<String,Object>)dataResult.getData();
        for(BranchReportVo vo:list){
            String benDepart = branchReportFormDto.getOrganizationalId()+"";
            if(benDepart.equals(vo.getOrganizationId())){
                if(roleType==1){
                    vo.setOrganizationName( nameMap.get(vo.getOrganizationId())+"(本部)");
                }else{
                    vo.setOrganizationName( nameMap.get(vo.getOrganizationId())+"");
                }
            }else{
                vo.setOrganizationName((String)nameMap.get(vo.getOrganizationId()));
            }
            vo.setLevel((Integer) nameMap.get(vo.getOrganizationId() + "_level"));
            vo.setIsSelf("1");
            vo.setPath((String)nameMap.get(vo.getOrganizationId()+"_path"));
        }
        return list;
    }

    public Object getMemberSalesReport(MemberReportDto memberReportDto){
        if(memberReportDto.getSearchName()!=null && !"".equals(memberReportDto.getSearchName())){
            DataResult dataResult = HttpUtil.httpGet(ProConstants.getSaleManagerIdListByName+memberReportDto.getOrganizationalId()+"/"+memberReportDto.getSearchName(), restTemplate);
            List<Long> saleManagerList = (List<Long>)dataResult.getData();
            if(saleManagerList !=null && saleManagerList.size()!=0){
                memberReportDto.setSearchIdList(saleManagerList);
            }else{
                memberReportDto.setSearchIdList(null);
                return new ArrayList<>();
            }
        }
        int roleType = memberReportDto.getRoleType();
        List<MemberSalesVo> list = new ArrayList<>();
        MemberSalesVo memberSalesVo = new MemberSalesVo();
        Map<String,Object> map = new HashMap<String, Object>();
        PageHelper.startPage(memberReportDto.getPageNo(), memberReportDto.getPageSize());
        switch(roleType){
            case 1:
                memberReportDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.memberSalesCountsHeadersGoods(memberReportDto);
                break;
            case 2:
                list = orderInfoMapper.memberSalesCountsBranchGoods(memberReportDto);
                break;
            case 3:
                list = orderInfoMapper.memberSalesCountsTwoLevelGoods(memberReportDto);
                break;
            case 4:
                list = orderInfoMapper.memberSalesCountsSubbranchIDGoods(memberReportDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryMemberDataFail);
        }
        int index = -1;
        //移除总部的数据 并且把userId
        List<Long> saleManagerList = new ArrayList<>();
        //查询出来没有数据
        if(list.size()!=0){
            for(int i=0;i<list.size();i++){
                if(list.get(i).getSaleId()==null|| 0==list.get(i).getSaleId() || "".equals(list.get(i).getSaleId())){
                    //表示有总部员工
                    memberSalesVo = list.get(i);
                    memberSalesVo.setSaleName("总部直属会员");
                    memberSalesVo.setOrganizationName("总部");
                    memberSalesVo.setContact("");
                    index = i;
                }else{
                    saleManagerList.add((list.get(i)).getSaleId());
                }
            }
        }else{
            //如果为空 则是直接返回了 表示一条数据都没有
            PageInfo<List<BranchReportVo>> page =  new PageInfo(list);
            map.put("list",page);
            map.put("pageCounts",0);
            return map;
        }
        //移除那条数据
        if(index != -1){
            list.remove(index);
        }
        //如果为空 则表示是有一条数据或者没有数据
        if(saleManagerList.size() == 0){
            if(index ==-1){
                //表示没有数据
                PageInfo<List<BranchReportVo>> page =  new PageInfo(list);
                map.put("list",page);
                map.put("pageCounts",0);
            }else{
                //表示只有一条数据
                list.add(memberSalesVo);
                PageInfo<List<BranchReportVo>> page =  new PageInfo(list);
                map.put("list",page);
                map.put("pageCounts",1);
            }
            return map;
        }
        //封装那些其他的字段 需要调用其他的服务
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE+"/userinfo/userInfoMain",saleManagerList, restTemplate);
        Map<Long,Map<String,Object>> userInfoMap = new HashMap<>();
        userInfoMap = (Map<Long,Map<String,Object>>)dataResult.getData();
        if(userInfoMap.size()!=0) {
            list = OrderReportFormService.getMapUserInfo(userInfoMap, list);
        }
        if(index != -1){
//            list.add(memberSalesVo);
            list.add(index,memberSalesVo);
        }
        PageInfo<List<BranchReportVo>> page =  new PageInfo(list);
        map.put("list",page);
        map.put("pageCounts",page.getTotal());
        return map;
        //**********?
    }

    public Object getMemberSalesReportExport(MemberReportDto memberReportDto){
        if(memberReportDto.getSearchName()!=null && !"".equals(memberReportDto.getSearchName())){
            DataResult dataResult = HttpUtil.httpGet(ProConstants.getSaleManagerIdListByName+memberReportDto.getOrganizationalId()+"/"+memberReportDto.getSearchName(), restTemplate);
            List<Long> saleManagerList = (List<Long>)dataResult.getData();
            if(saleManagerList !=null && saleManagerList.size()!=0){
                memberReportDto.setSearchIdList(saleManagerList);
            }else{
                memberReportDto.setSearchIdList(null);
                return new ArrayList<>();
            }
        }
        int roleType = memberReportDto.getRoleType();
        List<MemberSalesVo> list = new ArrayList<>();
        MemberSalesVo memberSalesVo = new MemberSalesVo();
        switch(roleType){
            case 1:
                memberReportDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.memberSalesCountsHeadersGoods(memberReportDto);
                break;
            case 2:
                list = orderInfoMapper.memberSalesCountsBranchGoods(memberReportDto);
                break;
            case 3:
                list = orderInfoMapper.memberSalesCountsTwoLevelGoods(memberReportDto);
                break;
            case 4:
                list = orderInfoMapper.memberSalesCountsSubbranchIDGoods(memberReportDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryMemberDataFail);
        }
        int index = -1;
        //移除总部的数据 并且把userId
        List<Long> saleManagerList = new ArrayList<>();
        //查询出来没有数据
        if(list.size()!=0){
            for(int i=0;i<list.size();i++){
                if(list.get(i).getSaleId()==null|| 0==list.get(i).getSaleId() || "".equals(list.get(i).getSaleId())){
                    //表示有总部员工
                    memberSalesVo = list.get(i);
                    memberSalesVo.setSaleName("总部直属会员");
                    memberSalesVo.setOrganizationName("总部");
                    memberSalesVo.setContact("");
                    index = i;
                }else{
                    saleManagerList.add((list.get(i)).getSaleId());
                }
            }
        }else{
            //如果为空 则是直接返回了 表示一条数据都没有
            return list;
        }
        //移除那条数据
        if(index != -1){
            list.remove(index);
        }
        //如果为空 则表示是有一条数据或者没有数据
        if(saleManagerList.size() == 0){
            if(index !=-1){
                //表示只有一条数据
                list.add(memberSalesVo);
            }
            return list;
        }
        //封装那些其他的字段 需要调用其他的服务
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE+"/userinfo/userInfoMain",saleManagerList, restTemplate);
        Map<Long,Map<String,Object>> userInfoMap = new HashMap<>();
        userInfoMap = (Map<Long,Map<String,Object>>)dataResult.getData();
        if(userInfoMap.size()!=0) {
            list = OrderReportFormService.getMapUserInfo(userInfoMap, list);
        }
        if(index != -1){
            list.add(index,memberSalesVo);
        }

        return list;
    }

    /**
     * 会员销售数据  根据goodsId
     * @param memberReportDto
     * @return
     */
    public Object getMemberSalesPort(MemberReportDto memberReportDto){
        int roleType = memberReportDto.getRoleType();
        int pageSize = memberReportDto.getPageSize() ;

        List<MemberSalesVo> directList = new ArrayList<>();
        MemberSalesVo memberSalesVo = null;
        //如果是总部进来 则需要 得到那条数据
        if(roleType==1&&memberReportDto.getPageNo()==1){
            //本部的第一页才需要 出了这种下面都会移除他
            directList =  orderInfoMapper.memberSalesCountsHeadersDirect(memberReportDto);
            if(directList!= null && directList.size()==1){
                if(directList.get(0)!=null){
                    directList.get(0).setContact("");
                    directList.get(0).setSaleName("总部直属员工");
                    directList.get(0).setOrganizationName("总部");
                }

            }
        }else{
            if(memberReportDto.getPageNo() == 1){
                memberReportDto.setPageSize(pageSize+1);
            }
        }
        System.out.println(memberReportDto.getPageSize()+"*****************");
        Map<String,Object> map = new HashMap<String, Object>();
        String goodsId = memberReportDto.getGoodsId();
        if(goodsId==null || "".equals(goodsId)){
            throw  CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }
        List<MemberSalesVo> list = new ArrayList<>();
        List<MemberSalesVo> listPage = new ArrayList<>();
        //封装page
        //设置分页处理
        //设置分页处理
        memberReportDto.setStartPage((memberReportDto.getPageNo()-1)*5); //limit 5,10  6-->10  pageSize
        memberReportDto.setEndPage(memberReportDto.getPageSize());
        //得到符合要求的
        switch(roleType){
            case 1:
                memberReportDto.setCompanyID(SystemContext.getCompanyID());
                list = orderInfoMapper.memberSalesCountsHeadersGoods(memberReportDto);
//                listPage = orderInfoMapper.memberSalesCountsHeadersGoodsPage(memberReportDto);
                break;
            case 2:
                list = orderInfoMapper.memberSalesCountsBranchGoods(memberReportDto);
//                listPage = orderInfoMapper.memberSalesCountsBranchGoodsPage(memberReportDto);
                break;
            case 3:
                list = orderInfoMapper.memberSalesCountsTwoLevelGoods(memberReportDto);
//                listPage = orderInfoMapper.memberSalesCountsTwoLevelGoodsPage(memberReportDto);
                break;
            case 4:
                list = orderInfoMapper.memberSalesCountsSubbranchIDGoods(memberReportDto);
//                listPage = orderInfoMapper.memberSalesCountsSubbranchIDGoodsPage(memberReportDto);
                break;
            default:
                throw CommonTools.createException(ErrorCodeEnum.queryMemberDataFail);
        }
        if(listPage.size() !=0){
            map.put("total",listPage.size());
        }
        //移除总部的数据 并且把userId
        List<Long> saleManagerList = new ArrayList<>();
        int index = -1;
        for(int i=0;i<list.size();i++){
            if(list.get(i).getSaleId()==null || "".equals(list.get(i).getSaleId()) || "0".equals(list.get(i).getSaleId()+"")){
                index = i;
            }else{
                saleManagerList.add((list.get(i)).getSaleId());
            }
        }
        int count = list.size();
        if(index != -1){
            memberSalesVo = list.get(index);
            memberSalesVo.setOrganizationName("总部直属员工");
            memberSalesVo.setSaleName("直属员工");
            list.remove(index);
        }



        //封装那些其他的字段 需要调用其他的服务
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE+"/userinfo/userInfoMain",saleManagerList, restTemplate);
        Map<Long,Map<String,Object>> userInfoMap = (Map<Long,Map<String,Object>>)dataResult.getData();
        if(userInfoMap != null && userInfoMap.size()!=0) {
            list = OrderReportFormService.getMapUserInfo(userInfoMap, list);
        }
            PageInfo<List<BranchReportVo>> page = null;
            if(directList!= null && directList.size()>=1 ){
                if(directList.get(0)==null)
                    directList.remove(0);
                if(list.size()!= count-1){
                    directList.add(memberSalesVo);
                }
                directList.addAll(list);
                list = directList;
                page = new PageInfo(list);

            }else{
                page = new PageInfo(list);;
            }
        page.setTotal(listPage.size());
        if(listPage.size()!=0){
            if(listPage.size()%pageSize==0){
                page.setPages(listPage.size()/pageSize);
            }else{
                page.setPages(listPage.size()/pageSize+1);
            }
//                page.setPages(?listPage.size()/pageSize:listPage.size()+1);
        }

            map.put("list",page);
            map.put("pageCounts",page.getPages());

        //填充list中 部分查询没有结果的属性
        return map;
    }

    //=========================zll =============================================
    /**
     * 根据商品 查询出款号以及名字
     * @param list
     * @return
     */
    public  List<GoodsReportAllVo> getGoodsInfo( List<GoodsReportAllVo>  list){
        if(list!=null){
            for(GoodsReportAllVo vo:list){
                GoodsInfo goods = goodsInfoMapper.selectByPrimaryKey(vo.getGoodsId());
                if(goods != null){
                    if(goods.getName() == null || "".equals(goods.getName())){
                        vo.setGoodsName("无名字");
                    }else{
                        vo.setGoodsName(goods.getName());
                    }
                    if(goods.getPrdNo() == null || "".equals(goods.getPrdNo())){
                        vo.setPrdNo("暂无款号");
                    }else{
                        vo.setPrdNo(goods.getPrdNo());
                    }
                }else{
                    throw CommonTools.createException(ErrorCodeEnum.queryDataFail);
                }
            }
        }
        return list;
    }


    //得到单个商品的销售
//   public Object getCountByGoodsId(Long goodsId){
//      return  orderItemMapper.selectCountByGodsId(goodsId);
//   }


//    public Object getSelfSaleData(){
//
//    }

    /**
     * 报表第一个部门的统计
     * @param memberReportDto
     * @return
     */
    public Object getOrderListPortHeaderSelf(MemberReportDto memberReportDto){
        Map<String,Object> map = new HashMap<String, Object>();
        String goodsId = memberReportDto.getGoodsId();
        if(goodsId==null || "".equals(goodsId)){
            throw  CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }

        int roleType = memberReportDto.getRoleType();
        int pageSize = memberReportDto.getPageSize();
        List<MemberSalesVo> directlyList = null;
        if(roleType == 1 && memberReportDto.getPageNo() == 1){
            memberReportDto.setCompanyID(SystemContext.getCompanyID());
            directlyList = orderInfoMapper.memberSalesCountsHeadersGoodsMember(memberReportDto);
            if(directlyList != null && directlyList.size() ==1){
                directlyList.get(0).setOrganizationName("总部");
                directlyList.get(0).setContact("");
                directlyList.get(0).setSaleName("总部直属会员");
                memberReportDto.setPageSize(pageSize-1);
            }
        }

        //封装page
        //设置分页处理
        PageHelper.startPage(memberReportDto.getPageNo(),memberReportDto.getPageSize());
        memberReportDto.setCompanyID(SystemContext.getCompanyID());
        List<MemberSalesVo> list = orderInfoMapper.memberSalesCountsHeadersGoodsSelf(memberReportDto);
        //添加所需要查询架构的list
        List<Long> saleManagerList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            saleManagerList.add((list.get(i)).getSaleId());
        }
        //封装那些其他的字段 需要调用其他的服务
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE+"/userinfo/userInfoMain",saleManagerList, restTemplate);
        Map<Long,Map<String,Object>> userInfoMap = (Map<Long,Map<String,Object>>)dataResult.getData();
        if(userInfoMap != null && userInfoMap.size()!=0) {
            list = OrderReportFormService.getMapUserInfo(userInfoMap, list);
        }
            PageInfo<List<BranchReportVo>> page = null;
            if(directlyList != null && directlyList.size() == 1){
                directlyList.addAll(list);
                page = new PageInfo(directlyList);
            }else{
                page = new PageInfo(list);
            }
            map.put("list",page);
            map.put("pageCounts",page.getPages());

        //填充list中 部分查询没有结果的属性
        return map;
    }

    public Object getOrderListPortHeaderSelfExport(MemberReportDto memberReportDto){
        String goodsId = memberReportDto.getGoodsId();
        if(goodsId==null || "".equals(goodsId)){
            throw  CommonTools.createException(ErrorCodeEnum.queryDataFail);
        }

        int roleType = memberReportDto.getRoleType();
        int pageSize = memberReportDto.getPageSize();
        List<MemberSalesVo> directlyList = null;
        if(roleType == 1 && memberReportDto.getPageNo() == 1){
            memberReportDto.setCompanyID(SystemContext.getCompanyID());
            directlyList = orderInfoMapper.memberSalesCountsHeadersGoodsMember(memberReportDto);
            if(directlyList != null && null !=directlyList.get(0) &&  directlyList.size() ==1){
                directlyList.get(0).setOrganizationName("总部");
                directlyList.get(0).setContact("");
                directlyList.get(0).setSaleName("总部直属会员");
                memberReportDto.setPageSize(pageSize-1);
            }
        }

        //封装page
        //设置分页处理
        memberReportDto.setCompanyID(SystemContext.getCompanyID());
        List<MemberSalesVo> list = orderInfoMapper.memberSalesCountsHeadersGoodsSelf(memberReportDto);
        //添加所需要查询架构的list
        List<Long> saleManagerList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            saleManagerList.add((list.get(i)).getSaleId());
        }
        //封装那些其他的字段 需要调用其他的服务
        DataResult dataResult = HttpUtil.httpPost(ProConstants.BASE+"/userinfo/userInfoMain",saleManagerList, restTemplate);
        Map<Long,Map<String,Object>> userInfoMap = (Map<Long,Map<String,Object>>)dataResult.getData();
        if(userInfoMap != null && userInfoMap.size()!=0) {
            list = OrderReportFormService.getMapUserInfo(userInfoMap, list);
        }
        if(directlyList != null && directlyList.size() == 1){
            directlyList.addAll(list);
            return directlyList;
        }else{
            return list;
        }
    }

    //========================create by zll 2017/05/24 ====================
    public Object getGoodsSkuReportGoodsId(GoodsReportDto goodsReportDto){
        if(goodsReportDto.getGoodsId() != null){
            List<Map<String,Object>> mapList = orderItemMapper.selectCountByGoodsSkuId(goodsReportDto);
            return mapList;
        }else{
            throw new ServiceException("商品有误", RestConst.ErrorCode.FORM_FAIL);
        }
    }

    public Object saleGoodsReportByName(MemberReportDto memberReportDto){
        //getSaleManagerIdListByName
        List<MemberSalesVo> list = new ArrayList<>();
        DataResult dataResult = HttpUtil.httpGet(ProConstants.getSaleManagerIdListByName+memberReportDto.getOrganizationalId()+"/"+memberReportDto.getSearchName(), restTemplate);
        List<Long> saleManagerList = (List<Long>)dataResult.getData();
        if(saleManagerList !=null && saleManagerList.size() !=0){
            memberReportDto.setSearchIdList(saleManagerList);
            list = orderInfoMapper.memberGoodsSalesById(memberReportDto);
        }
        if(list.size() != 0){
            DataResult dataResult2 = HttpUtil.httpPost(ProConstants.BASE+"/userinfo/userInfoMain",saleManagerList, restTemplate);
            Map<Long,Map<String,Object>> userInfoMap = new HashMap<>();
            userInfoMap = (Map<Long,Map<String,Object>>)dataResult2.getData();
            if(userInfoMap.size()!=0) {
                list = OrderReportFormService.getMapUserInfo(userInfoMap, list);
            }
        }

        return list;
    }
}
